package com.aabte.lock.singlelock;


import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Daniel
 */
public class MutexTest {

    public static void main(String[] args) throws InterruptedException {

        Mutex lock = new Mutex();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    lock.lock();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        // nothing
                    }
                    System.err.println(Thread.currentThread().getName());
                    lock.unlock();
                    break;

                }
            }
        };

        List<Thread> threads = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(runnable);
            thread.setName("test-lock-" + i);
            thread.start();
            threads.add(thread);
        }

        for (Thread thread : threads) {
            thread.join();
        }
    }


}
